//
//  ViewController.m
//  Example12-1
//
//  Created by 孙晓晔 on 2017/4/18.
//  Copyright © 2017年 孙晓晔. All rights reserved.
//

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()<CLLocationManagerDelegate>
@property(nonatomic,strong) CLLocationManager *manager1;
@property(nonatomic,strong) CLGeocoder *coder;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
    
    if (status == kCLAuthorizationStatusNotDetermined) {
        [self.manager1 requestAlwaysAuthorization];
        
    } else if (status == kCLAuthorizationStatusAuthorizedAlways) {
        self.manager1.delegate = self;
        
        [self.manager1 startUpdatingLocation];
    }
}

-(CLLocationManager *)manager1 {
    if (_manager1 == nil) {
        _manager1 = [[CLLocationManager alloc]init];
    }
    
    return _manager1;
}
-(CLGeocoder *)coder {
    if (_coder == nil) {
        _coder = [[CLGeocoder alloc]init];
        
    }
    
    return _coder;
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    
    
    CLLocation *location1 = [locations lastObject];
    
//
    NSLog(@"定位成功。经度 = %f, 纬度 = %f, 高度 = %f, 方向 = %f, 速度 = %f, 时间 = %@ ",
          location1.coordinate.longitude, location1.coordinate.latitude, location1.altitude,location1.course, location1.speed,location1.timestamp);
    
    [self.coder reverseGeocodeLocation:location1 completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (error == nil) {
            CLPlacemark *mark = [placemarks firstObject];
            
            NSDictionary *dict = mark.addressDictionary;
            
            NSLog(@"%@",dict);
        }
    }];
    
    [self.manager1 stopUpdatingLocation];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
